package com.stylefeng.guns.modular.pos.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.pos.service.IPosService;
import com.stylefeng.guns.modular.system.model.Pos;
import com.stylefeng.guns.modular.system.warpper.PosWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * POS管理控制器
 *
 * @author fengshuonan
 * @Date 2018-05-30 16:52:59
 */
@Controller
@RequestMapping("/pos")
public class PosController extends BaseController {
    private String PREFIX = "/pos/pos/";
    @Autowired
    private IPosService posService;

    /**
     * 跳转到POS管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "pos.html";
    }

    /**
     * 跳转到添加POS管理
     */
    @RequestMapping("/pos_add")
    public String posAdd() {
        return PREFIX + "pos_add.html";
    }

    /**
     * 跳转到修改POS管理
     */
    @RequestMapping("/pos_update/{posId}")
    public String posUpdate(@PathVariable Integer posId, Model model) {
        Pos pos = posService.selectById(posId);
        model.addAttribute("item",pos);
        LogObjectHolder.me().set(pos);
        return PREFIX + "pos_edit.html";
    }

    /**
     * 获取POS管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String,Object>> posList=posService.posList();
        return new PosWarpper(posList).warp();
    }

    /**
     * 新增POS管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Pos pos) {
        posService.insert(pos);
        return SUCCESS_TIP;
    }

    /**
     * 删除POS管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer posId) {
        posService.deleteById(posId);
        return SUCCESS_TIP;
    }

    /**
     * 修改POS管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Pos pos) {
        posService.updateById(pos);
        return SUCCESS_TIP;
    }

    /**
     * POS管理详情
     */
    @RequestMapping(value = "/detail/{posId}")
    @ResponseBody
    public Object detail(@PathVariable("posId") Integer posId) {
        return posService.selectById(posId);
    }
}
